package com.xhj.controller;

import com.xhj.entity.Permission;
import com.xhj.service.PermissionService;
import com.xhj.util.ResultEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 权限(Permission)表控制层
 *
 * @author makejava
 * @since 2021-03-15 12:26:25
 */
@RestController
@RequestMapping("/admin/acl/permission")
//@CrossOrigin
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    public ResultEntity getAllPermission(){
        List<Permission> list = permissionService.queryAllMenu();
        return ResultEntity.successWithData().data("list",list);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public ResultEntity deletePermission(@PathVariable("id")String id){
        permissionService.removeMenu(id);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("doAssign")
    public ResultEntity doAssign(String roleId,String[] permissionId){
        permissionService.saveRolePermissionRelationShip(roleId,permissionId);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public ResultEntity savePermission(@RequestBody Permission permission){
        permissionService.save(permission);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public ResultEntity updatePermission(@RequestBody Permission permission){
        permissionService.updateById(permission);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public ResultEntity toAssign(@PathVariable("roleId") String roleId){
        List<Permission> list = permissionService.queryMenuByRoleId(roleId);
        return ResultEntity.successWithData().data("list",list);
    }
}